//
//  MainTabBarController.swift
//  weibo
//
//  Created by 孙新 on 16/4/16.
//  Copyright © 2016年 孙新. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //创建一个闭包
        let composeClickButton = {
            print("点击按钮弹出控制器")
  
        }
        
      
        

        //导入自定义tarBar
        let tabBar = TabBarView()
        tabBar.composeButtonClickClosure = composeClickButton
        
        //通过KVC设置tabBar的属性
        self.setValue(tabBar, forKey: "tabBar")
        
        //调用添加控制器的方法将四个控制器一一设置
        addChildViewController(HomeTableController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(DiscoverTableController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableController(), title: "我", imageName:"tabbar_profile")
        
    }

  //添加tabBar的子控制器
    func addChildViewController(vc : UIViewController, title : String, imageName :String) {
        //设置自控制器tabBar下的文字
        vc.title = title
        
        //设置tabBar下的图片
        vc.tabBarItem.image = UIImage(named: imageName)
        //设置选中时的图片为图片原始图样
        vc.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        
        //设置文字颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Selected)
        //先将vc控制器添加到导航控制器上 并成为导航控制器的根控制器 然后成为tarBar控制器的子控制器
        addChildViewController(UINavigationController(rootViewController: vc))
    }
    



}
